package com.study.sxc.controller;

import com.study.sxc.Entity.MachineType;
import com.study.sxc.enums.ResponseCode;
import com.study.sxc.response.Response;
import com.study.sxc.service.machineTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api/machineType")
public class MachineTypeController {

    @Resource
    private machineTypeService machineTypeService;

    @GetMapping("/get")
    public Response<List<MachineType>> getMachineTypes() {
        List<MachineType> types;
        try {
            types = machineTypeService.getAllMachineTypes();
        } catch (Exception e) {
            return Response.<List<MachineType>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info("查询异常: " + e.getMessage())
                    .data(null)
                    .build();
        }
        if(types == null || types.isEmpty()) {
            return Response.<List<MachineType>>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(types)
                    .build();
        }
        return Response.<List<MachineType>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(types)
                .build();
    }

    @PostMapping("/add")
    public Response<Void> addMachineType(@RequestBody MachineType machineType) {
        // 校验必要字段
        if (machineType.getTypeName() == null || machineType.getTypeName().trim().isEmpty()) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("机型名称不能为空")
                    .build();
        }
        int result = machineTypeService.addMachineType(machineType);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @PutMapping("/update")
    public Response<Void> updateMachineType(@RequestBody MachineType machineType) {
        // 校验必要字段
        if (machineType.getTypeId() == null || machineType.getTypeId() <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("机型ID不能为空且必须为正整数")
                    .build();
        }
        int result = machineTypeService.updateMachineType(machineType);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @DeleteMapping("/delete")
    public Response<Void> deleteMachineType(@RequestBody MachineType machineType) {
        Integer id = machineType.getTypeId();
        if (id == null || id <= 0) {
            return Response.<Void>builder()
                    .code("9999")
                    .info("机型ID不能为空且必须为正整数")
                    .build();
        }
        int result = machineTypeService.deleteMachineType(id);
        if (result > 0) {
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } else {
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @GetMapping("/get/{id}")
    public Response<MachineType> getMachineTypeById(@PathVariable Integer id) {
        // 根据id获取机型信息
        MachineType type = machineTypeService.getMachineTypeById(id);
        if (type == null) {
            return Response.<MachineType>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(null)
                    .build();
        }
        return Response.<MachineType>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(type)
                .build();
    }
} 